﻿' handle fossil command line
'
Imports System.IO

Public Class fossilCommand
	
	Public currentDirectory As String
	
	Public Sub New()
		Me.currentDirectory = CurDir()
	End Sub
	
	Public Function changeDirectory(path As String) As Boolean
		Dim dir As New DirectoryInfo(path)
		If dir.Exists Then
			chdir(path)
			me.currentDirectory = path
			Return True
		Else
			msgbox("directory '" & path & "' is not found")
		End If
		Return False
	End Function
	
	Public Function execAddRemove() As Boolean
		me.exec("addremove")
	End Function
	
	Public Function execChanges() As Collection
		
		Dim files As New Collection
		Dim file As New fossilFile
		Dim commandOutput As String 
		Dim commandString As String
		
		commandString = "changes --sha1sum"
		commandOutput = Me.exec(commandString)
		
		debug.Print(commandOutput)
		file.path = "class/fossilGuiChangesGtkTreeView.php"
		file.status = "EDITED"
		files.Add(file)
		
		Return files
	End Function
	
    Public Function close() As Boolean
        Me.exec("close --force")
        Return True
    End Function
	
	Public Function execInfo() As fossilInfo
		Dim info As New fossilInfo
		info.repository = "D:/documents/projects/php/helpdesk/.fossil/repository"
		Return info
	End Function
	
    Private Function exec(ByVal Command As String) As String

        Dim CMD As New Process
        Dim SW As StreamWriter
        Dim SR As StreamReader
        Dim SRE As StreamReader
        Dim Output As String
        Dim OutputError As String

        CMD.StartInfo.FileName = "fossil"
        CMD.StartInfo.Arguments = Command
        CMD.StartInfo.UseShellExecute = False
        CMD.StartInfo.RedirectStandardInput = True
        CMD.StartInfo.RedirectStandardOutput = True
        CMD.StartInfo.RedirectStandardError = True
        CMD.StartInfo.CreateNoWindow = True
        CMD.Start()

        SW = CMD.StandardInput
        SR = CMD.StandardOutput
        SRE = CMD.StandardError

        SW.WriteLine(Command)

        Output = ""
        Do Until SR.EndOfStream = True
            Output = Output & SR.ReadLine
            Output = Output & Environment.NewLine
        Loop

        OutputError = ""
        Do Until SRE.EndOfStream = True
            OutputError = Output & SRE.ReadLine
            OutputError = Output & Environment.NewLine
        Loop


        CMD.Dispose()
        SW.Dispose()

        SR.Dispose()
        SRE.Dispose()

        Debug.Print(">>> fossil " & Command)
        If Len(Output) > 0 Then
            Debug.Print(Output)
        Else
            If Len(OutputError) > 0 Then
                Debug.Print("ERROR ! " & OutputError)
            End If
        End If

        Return Output
    End Function
	
End Class
